package com.jl.card.filesystem;

import java.util.HashMap;

import com.jl.card.Card;
import com.jl.card.SWException;
import com.jl.card.file.CardFilePath;
import com.jl.card.provider.jl3g.JL3GFileAccess;
import com.jl.util.HexUtil;

public class EFarrFactory {

	public static HashMap<String, IEF> arrs = new HashMap<String, IEF>(2);

	public static IEF getEFarr(Card card, short id) {
		if (containKey(card, id)) {
			return arrs.get(makeID(card, id));
		} else {
			synchronized (EFarrFactory.class) {
				JL3GFileAccess access = new JL3GFileAccess(new CardFilePath(
						new short[] { id }), card);
				IEF arr = null;
				try {
					arr = access.intiEFarr();
				} catch (SWException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				arrs.put(makeID(card, id), arr);
				return arr;
			}
		}
	}

	public static boolean containKey(Card card, short id) {
		return arrs.containsKey(makeID(card, id));
	}

	private static String makeID(Card card, short id) {
		return card.getName() + HexUtil.toHex(id & 0xFFFF, 2);
	}
}
